package com.thinkingstar.iads.datacenter.entity.dao;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcCRHInfo;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  数据中心_动车设施信息表 维护
 */
@IocBean(args = {"refer:dao"})
public class DaoDcCRHInfo extends EntityRepositoryServiceImpl<DcCRHInfo> {
    public DaoDcCRHInfo(Dao dao) {
        super(dao);
    }


    /**
     *   查询分页显示
     * @param nutMap
     * @return
     */
    public QueryResult findDcCRHInfo(NutMap nutMap){
        List<Map> page_list = null;
        Pager pager = null;
        StringBuffer final_sql = new StringBuffer(" select dci.*  from dc_crh_info dci  where dci.del_flag = '0' ");
        if(nutMap.get("crh_info_name") != null){
            final_sql.append(" and dci.crh_info_name = @crh_info_name");
        }
        Sql sql = Sqls.create(final_sql.toString());
        if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
            pager = new Pager(nutMap.getInt("page_num"),nutMap.getInt("page_size"));
            pager.setRecordCount(findDcCRHNum(nutMap));
            sql.setPager(pager);
        }
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List <Map> list = new ArrayList<>();
                while (rs.next()) {
                    Map <String , Object> map = new HashMap<>();
                    map.put("id", rs.getString("id"));
                    map.put("crh_info_name", rs.getString("crh_info_name"));
                    map.put("crh_info_type", rs.getString("crh_info_type"));
                    map.put("crh_info_size", rs.getString("crh_info_size"));
                    map.put("crh_info_city", rs.getString("crh_info_city"));
                    map.put("crh_info_department", rs.getString("crh_info_department"));
                    map.put("crh_info_longitude", rs.getString("crh_info_longitude"));
                    map.put("crh_info_latitude", rs.getString("crh_info_latitude"));
                    map.put("crh_info_attach", rs.getString("crh_info_attach"));
                    list.add(map);
                }
                SwapUtil.clearNull(list);
                return list;
            }
        });
        dao().execute(sql);
        page_list = sql.getList(Map.class);
        return new QueryResult(page_list, pager);
    }

    /**
     *  查询信息的总条数
     * @param nutMap
     * @return
     */

    public int findDcCRHNum(NutMap nutMap){
        StringBuffer final_sql = new StringBuffer(" select count(dci.id)  from dc_crh_info dci  where dci.del_flag = '0' ");
        if(nutMap.get("crh_info_name") != null){
            final_sql.append(" and dci.crh_info_name = @crh_info_name");
        }
        Sql sql = Sqls.create(final_sql.toString());

        sql.setCallback(new SqlCallback() {
            int num = 0;
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                while (rs.next()) {
                    num = rs.getInt("num");
                }
                return num;
            }
        });
        dao().execute(sql);
        return sql.getInt();
    }

}
